import Foundation

// MARK: 栈
protocol Stack {
    /// 持有的元素类型
    associatedtype Element
    
    /// 是否为空
    var isEmpty: Bool { get }
    /// 栈的大小
    var size: Int { get }
    /// 栈顶元素
    var peek: Element? { get }
    
    /// 进栈
    mutating func push(_ newElement: Element)
    /// 出c栈
    mutating func pop() -> Element?
}

public struct IntegerStack: Stack {
    public typealias Element = Int
    
    private var stack = [Element]()
    
    var isEmpty: Bool { return stack.isEmpty }
    var size: Int { return stack.count }
    var peek: Element? { return stack.last }
    
    public mutating func push(_ newElement: Element) {
        stack.append(newElement)
    }
    
    public mutating func pop() -> Element? {
        return stack.popLast()
    }
}
